// pages/shop_add/index.js
var md5 = require('../../utils/md5.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_hangye:0,
    order_id:0,
    updataImgShop:'https://hongbao.zntech-china.com/attachment/images/wxcx//upload.png',
    updataImgPic: 'https://hongbao.zntech-china.com/attachment/images/wxcx//upload.png',
    region: ['山东省', '烟台市', '莱山区'],
    startTime:'08:30',
    endTime: '21:30',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      shop_hangye: options.id
    });
    //console.log(that.data)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })

  },
  /**
   * 用户友好交互界面
   */
  chooseImageShop: function () {
    let _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#e64340",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            _this.chooseWxImageShop('album')//从相册选择
          } else if (res.tapIndex == 1) {
            _this.chooseWxImageShop('camera')//调用拍照
          }
        }
      }
    })
  },
   /**
   * 调用图片接口
   */
  chooseWxImageShop: function (type) {
    let _this = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        _this.setData({
          updataImgShop: res.tempFilePaths[0],
        })
      }
    })
  },
  /**
   * 用户友好交互界面
   */
  chooseImagePic: function () {
    let _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#e64340",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            _this.chooseWxImagePic('album')//从相册选择
          } else if (res.tapIndex == 1) {
            _this.chooseWxImagePic('camera')//调用拍照
          }
        }
      }
    })
  },
  /**
  * 调用图片接口
  */
  chooseWxImagePic: function (type) {
    let _this = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        //console.log(res)
        _this.setData({
          updataImgPic: res.tempFilePaths[0],
        })
      }
    })
  },
  bindSave:function(e){
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    var openid = wx.getStorageSync('openid');
    var order_id = wx.getStorageSync('shop_order_id');
    var city_id = 49;
    var shop_hangye = that.data.shop_hangye
    var shop_name = e.detail.value.shop_name
    var shop_fu_name = e.detail.value.shop_fu_name
    var shop_pic_small = that.getupImgs(that.data.updataImgShop)
    var shop_address = e.detail.value.shop_address
    var shop_address_jingwei = wx.getStorageSync('latitude') + ',' + wx.getStorageSync('longitude')
    var shop_special = e.detail.value.shop_special
    var shop_content = e.detail.value.shop_content
    var shop_worktime = that.data.startTime + '-' + that.data.endTime
    var shop_wechat = 123
    var shop_wechat_qrcode = that.getupImgs(that.data.updataImgShop)
    var shop_cert_img = that.getupImgs(that.data.updataImgPic)
    var shop_tel = e.detail.value.shop_tel
    var shop_tel1 = e.detail.value.shop_tel1
    var m_id = 0
    var consume = e.detail.value.consume
    var picture = '123'
    if (shop_name == "") {
      wx.showModal({
        title: '错误',
        content: '请填写店铺名称',
        showCancel: false
      })
      return
    }
    if (shop_tel == "") {
      wx.showModal({
        title: '错误',
        content: '请填写联系电话',
        showCancel: false
      })
      return
    }
    if (shop_content == "") {
      wx.showModal({
        title: '错误',
        content: '请填写店铺介绍',
        showCancel: false
      })
      return
    }
    if (shop_special == "") {
      wx.showModal({
        title: '错误',
        content: '请填写店铺特色',
        showCancel: false
      })
      return
    }
    if (consume == "") {
      wx.showModal({
        title: '错误',
        content: '请填写人均消费',
        showCancel: false
      })
      return
    }
    if (shop_address == "") {
      wx.showModal({
        title: '错误',
        content: '请填写店铺地址',
        showCancel: false
      })
      return
    }

    var service = 'Shop.AddShop';
    var sign = md5.md5(city_id + consume + m_id + openid + order_id + picture + service + shop_address + shop_address_jingwei + shop_cert_img + shop_content + shop_fu_name + shop_hangye + shop_name + shop_pic_small + shop_special + shop_tel + shop_tel1 + shop_wechat + shop_wechat_qrcode + shop_worktime + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        openid: openid,
        order_id: order_id,
        city_id: city_id,
        shop_hangye: shop_hangye,
        shop_name: shop_name,
        shop_fu_name: shop_fu_name,
        shop_pic_small: shop_pic_small,
        shop_address: shop_address,
        shop_address_jingwei: shop_address_jingwei,
        shop_special: shop_special,
        shop_content: shop_content,
        shop_worktime: shop_worktime,
        shop_wechat: shop_wechat,
        shop_wechat_qrcode: shop_wechat_qrcode,
        shop_cert_img: shop_cert_img,
        shop_tel: shop_tel,
        shop_tel1: shop_tel1,
        m_id: m_id,
        consume: consume,
        picture: picture
      },
      success: function (res) {
        //console.log(res.data.data.code)
        if (res.data.data.code == 0) {
          wx.showToast({
            title: '入驻成功',
            icon: 'success',
            duration: 2000
          })
          wx.switchTab({
            url: "/pages/ucenter/index/index"
          })
        }else{
          wx.showToast({
            title: '入驻失败',
            icon: 'cancel',
            duration: 2000
          })
        }
      }
    })
  },

  //上传图片到服务器端
  getupImgs:function(e){
    var service = 'System.uploadImgWxa'
    var file = e
    var sign = md5.md5(service);
    wx.uploadFile({
      url: app.globalData.apiUrl + '?service=' + service, //仅为示例，非真实的接口地址
      filePath: e,
      name: 'file',
      formData: {
        'sign': sign,
      },
      success: function (res) {
        if (res.data.code == 0){
          return res.data.info.url;
        }
      }
    })
  }
})